package com.chazz.servlet;

import com.alibaba.fastjson.JSONArray;
import com.chazz.entity.Adress;
import com.chazz.service.impl.AdressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/adress.do")
public class AdressServlet extends HttpServlet {
    AdressServiceImpl asi=new AdressServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String op=req.getParameter("option");
        if(op.equals("adressList")){
                adressList(req,resp);
        }else if(op.equals("add")){
            addAdress(req,resp);
        }else if(op.equals("del")){
            delAdress(req,resp);
        }else if(op.equals("up")){
            upAdress(req,resp);
        }
    }

    private void upAdress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String adressId=req.getParameter("id");
        int id=Integer.parseInt(adressId);
        String adressVallue=req.getParameter("info");
        resp.setContentType("text/plain;charset=utf-8");
        PrintWriter out=resp.getWriter();
        if(asi.updateAdress(id,adressVallue)){
            out.write("true");
        }else{
            out.write("false");
        }
    }

    private void delAdress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String adressId=req.getParameter("id");
        int id=Integer.parseInt(adressId);
        resp.setContentType("text/plain;charset=utf-8");
        PrintWriter out=resp.getWriter();
        if(asi.deletAdress(id)){
            out.write("true");
        }else{
            out.write("false");
        }

    }

    private void addAdress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id=req.getParameter("uid");
        int uid=Integer.parseInt(id);
        String adressValue=req.getParameter("info");
        resp.setContentType("text/plain;charset=utf-8");
        PrintWriter out=resp.getWriter();
        if(asi.addAdress(uid,adressValue)){
            out.write("true");
        }else{
            out.write("false");
        }
        out.close();

    }

    private void adressList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id=req.getParameter("uid");
        int uid=Integer.parseInt(id);
        List<Adress> adressList;
        adressList=asi.showAdressList(uid);
        String adressListToJson= JSONArray.toJSONString(adressList);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.write(adressListToJson);
        out.close();
        return;

    }
}

